package com.ruimei.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

public class FileUploadUtil
{
  public static List<String> checkType(String str, HttpServletRequest req)
  {
    List<String> list = null;
    if (str.equals("icon")) {
      list = saveFiles(req, "icon");
    } else if (str.equals("consultant")) {
      list = saveFiles(req, "consultant");
    } else if (str.equals("train")) {
      list = saveFiles(req, "train");
    } else {
      list = saveFiles(req, "team");
    }
    return list;
  }
  
  public static List<String> saveFiles(HttpServletRequest req, String type)
  {
    List<String> fileNameSet = new ArrayList();
    
    CommonsMultipartResolver multipartReslver = new CommonsMultipartResolver(req.getSession().getServletContext());
    if (multipartReslver.isMultipart(req))
    {
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)req;
      
      Iterator<String> iter = multiRequest.getFileNames();
      while (iter.hasNext())
      {
        MultipartFile file = multiRequest.getFile(((String)iter.next()).toString());
        if (file != null)
        {
          String filename = saveFile(req, file, type);
          fileNameSet.add(filename);
        }
      }
    }
    return fileNameSet;
  }
  
  public static String saveFile(HttpServletRequest req, MultipartFile file, String type)
  {
    String path = null;
    
    String fileName = file.getOriginalFilename();
    if (type.equals("icon"))
    {
      path = "d://upload/icon";
    }
    else if (type.equals("consultant"))
    {
      path = "d://upload/consultant";
    }
    else if (type.equals("train"))
    {
      path = "d://upload/train";
    }
    else if (type.equals("team"))
    {
      path = "d://upload/team";
    }
    else
    {
      path = "d://upload/ueditor";
    }
    File dir = new File(path);
    if (!dir.exists()) {
      dir.mkdirs();
    }
    File newFile = new File(path, fileName);
    try
    {
      file.transferTo(newFile);
    }
    catch (IllegalStateException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    String str = path + "/" + fileName;
    return str.replaceAll("\\\\", "/");
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruime\\util\FileUploadUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */